package com.gdk.ksoft.ui.mores;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.ConstantData;
import com.gdk.ksoft.ui.logins.LoginActivity;
import com.gdk.ksoft.ui.logins.DownloadTableActivity;
import com.gdk.ksoft.assembly.widget.CircleProgressBar;
import com.gdk.ksoft.assembly.utils.system.ApkUtils;
import com.gdk.ksoft.assembly.utils.system.ConfigUtils;
import com.gdk.ksoft.assembly.utils.system.FileUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.File;

/**
 * 推送版本更新页面
 */
public class UpdateActivity extends Activity {

    @ViewInject(R.id.pupdate_cpbDownload)private CircleProgressBar cpbDownload;//下载进度条
    @ViewInject(R.id.pupdate_txtUpdateInfo)private TextView txtUpdateInfo;//更新信息
    @ViewInject(R.id.pupdate_txtMaxValue)private TextView txtMaxValue;//软件最大值
    @ViewInject(R.id.pupdate_txtProgressValue)private TextView txtProgressValue;//软件下载进度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        ViewUtils.inject(this);
        txtUpdateInfo.setText(R.string.update_information);
        Intent intent = getIntent();
        if(intent!=null && intent.hasExtra("downloadUrl")){//包含下载地址则进行下载
            downloadApk(intent.getStringExtra("downloadUrl"));
        }
    }

    /**
     * 下载软件安装包
     * @param downloadUrl 下载地址
     */
    public void downloadApk(final String downloadUrl){
        new HttpUtils().download(downloadUrl, ConstantData.DOWNLOAD_PATH, new RequestCallBack<File>() {

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
                txtMaxValue.setText("总长度：" + FileUtils.getFileSize(total));
                txtProgressValue.setText("已下载：" + FileUtils.getFileSize(current));
                cpbDownload.setMax(total);//设置最大值
                cpbDownload.setProgress(current);//设置当前进度
            }

            @Override
            public void onSuccess(ResponseInfo<File> fileResponseInfo) {
                if (fileResponseInfo.result != null) {
                    try {
                        ConfigUtils.getInstance(UpdateActivity.this).getEditor()//升级后需要重新下载数据，所以会清空其数据下载日期的记录
                                .putString(LoginActivity.PASSWORD_FIELD,"")//清空登录密码
                                .putString(DownloadTableActivity.DOWNLOAD_DATE_TIME, "").commit();
                        ApkUtils.installApk(UpdateActivity.this, fileResponseInfo.result.getAbsolutePath());
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(UpdateActivity.this, "执行安装失败!原因：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    UpdateActivity.this.finish();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                if(UpdateActivity.this == null) return;
                Toast.makeText(UpdateActivity.this, "下载更新失败!原因：" + s, Toast.LENGTH_SHORT).show();
                UpdateActivity.this.finish();
            }
        });
    }
}
